//
//  BindDeviceViewController.m
//  Router
//
//  Created by teki on 15/11/3.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "BindDeviceViewController.h"
#import "AppDelegate.h"
#import "BindWIFIListViewController.h"

#import "DiscoverHelper.h"

@interface BindDeviceViewController ()
@property (weak, nonatomic) IBOutlet UILabel *deviceLabel;
@property (weak, nonatomic) IBOutlet UIButton *bindNowButton;


@property (nonatomic, strong) DiscoverHelper *discoverHelper;


@end

@implementation BindDeviceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"绑定设备";
    
    if (self.responseDict)
    {
        self.ssid =  self.responseDict[@"ssid"];
        self.deviceID = self.responseDict[@"deviceID"];
        self.deviceLabel.text = self.ssid;

    }
    

    self.discoverHelper = [[DiscoverHelper alloc] init];
    self.bindNowButton.layer.cornerRadius = 5.0f;
    self.bindNowButton.layer.masksToBounds = YES;
    [self.bindNowButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.bindNowButton setBackgroundImage:[UIImage imageWithColor:NavgationBarBackgroundColor] forState:UIControlStateNormal];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark   IBActions
- (IBAction)bindNowButtonPressed:(id)sender {
    
    if (self.ssid && self.deviceID)
    {
        self.activity = [self startActivityWithTitle:@"正在绑定"];
        UserModel *user = [UserSingleton shareInstance].userModel.user[0];
        [self.discoverHelper bindUserAndDeviceWithDeviceID:self.deviceID User:user.userName SSID:self.ssid success:^(BOOL succ, id responseObject, NSString *msg) {
            if (responseObject)
            {
                NSString *status = responseObject[@"deviceStatus"];
                if ([status isEqualToString:@"DeviceUpdate"])
                {
                    [self stopActivityWithActivity:self.activity title:@"绑定成功!"];

                    LoginResponseModel *model = [LoginResponseModel objectWithKeyValues:responseObject];
                    [UserSingleton shareInstance].userModel.bindDevice = model.bindDevice;
                    [UserSingleton shareInstance].userModel.wifi = model.wifi;
                    
                    NSArray *array = [model.stations sortedArrayUsingComparator:^NSComparisonResult(StationModel *  _Nonnull obj1, StationModel*  _Nonnull obj2) {
                        NSNumber *number1 = @(obj1.master);
                        NSNumber *number2 = @(obj2.master);
                        return [number2 compare:number1];
                    }];
                    [UserSingleton shareInstance].userModel.stations = array;
                    [UserSingleton shareInstance].userModel.rate = model.rate;
                    [[UserSingleton shareInstance] saveData];
                    
                    
                    [self dispatchTimerWithTime:2.0 block:^{
                        UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                        UIViewController *mainVC = [main instantiateViewControllerWithIdentifier:@"MainVC"];
                        ApplicationDelegate.window.rootViewController = mainVC;
 
                    }];
                }
                else
                {
                    [self stopActivityWithActivity:self.activity title:@"绑定失败!"];
                }
            }
            else
            {
                [self stopActivityWithActivity:self.activity title:@"绑定失败!"];
            }

        } failure:^(NSError *error) {
            NSString *failedReason = [error localizedDescription];
            [self stopActivityWithActivity:self.activity title:failedReason];
        }];

    }
    else
    {
        [self promptMessage:@"数据格式有误"];
    }
 
}






@end
